package com.dianban.controller.nengliang;

import com.dianban.base.ResultCode;
import com.dianban.base.ResultVo;
import com.dianban.config.redis.RedisConstant;
import com.dianban.config.redis.RedisUtil;
import com.dianban.controller.BaseController;
import com.dianban.entity.StoreManager;
import com.dianban.entity.User;
import com.dianban.service.StoreManagerService;
import com.dianban.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(value = "ApiStoreManagerController", description = "项目店长控制器")
@RestController
@RequestMapping(value = "${interfacePath}/nengliang/manager/")
public class ApiStoreManagerController extends BaseController {

    @Autowired
    private StoreManagerService storeManagerService;

    @ApiOperation(notes = "add", httpMethod = "POST", value = "项目店长入驻")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userToken", value = "用户token", required = true, paramType = "header", dataType = "string"),
            @ApiImplicitParam(name = "storeId", value = "门店Id", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "name", value = "姓名", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "province", value = "省", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "city", value = "市", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "area", value = "区", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "address", value = "详细地址", required = true, paramType = "query", dataType = "string"),
    })
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ResultVo add(String storeId, String name, String phone, String province, String city, String area, String address, HttpServletRequest request, HttpServletResponse response) {
        String userToken = request.getHeader(UserUtils.USER_TOKEN);
        String key = RedisConstant.USER_TOKEN_KEY + userToken;
        User user = (User) RedisUtil.get(key);
        if (user == null) {
            return ResultVo.error(ResultCode.ERROR, "请求头userToken过期");
        }
        try {
            StoreManager storeManager = new StoreManager();
            storeManager.setUserId(user.getId());
            storeManager.setStoreId(Long.valueOf(storeId));
            storeManager.setName(name);
            storeManager.setPhone(phone);
            storeManager.setAddress(address);
            storeManager.setProvinceName(province);
            storeManager.setCityName(city);
            storeManager.setAreaName(area);
            storeManager.setStatus(0);
            storeManagerService.insert(storeManager);
        } catch (Throwable e) {
            e.printStackTrace();
            return ResultVo.error(ResultCode.ERROR, "操作失败");
        }
        return ResultVo.success("", "操作成功");
    }

}
